﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using ExtremeML.Packaging;
using NUnit.Framework;

namespace ExtremeML.Tests
{
	[TestFixture]
	public class ResizeTemplateWorksheet
	{
		[Test]
		public void InsertRowsAboveTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertRows(1, 5); // insert 5 rows starting on 0-based row 1
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(7, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(11, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(11, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C8:H12", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void InsertRowsStartingOnFirstTableRow()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertRows(2, 5); // insert 5 rows starting on 0-based row 2
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(7, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(11, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(11, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C8:H12", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}
		
		[Test]
		public void InsertRowsWithinTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertRows(3, 5); // insert 5 rows starting on 0-based row 2
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(11, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(9, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(11, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H12", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void InsertRowsBelowTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertRows(7, 5); // insert 5 rows starting on 0-based row 2

				// This test fails because cell styles from the table are propagated.
				// The propagation code needs to be more selective.
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");

				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void DeleteRowsAboveTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteRows(0, 2); // delete 2 rows starting on 0-based row 0
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(0, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(0, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C1:H5", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		[ExpectedException(typeof(InvalidOperationException))]
		public void DeleteRowsSpanningUpperTableBoundary()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteRows(1, 2); // delete 2 rows starting on 0-based row 1
			}
		}

		[Test]
		public void DeleteRowsSpanningEntireTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteRows(1, 7); // delete 7 rows starting on 0-based row 1
				Assert.AreEqual(0, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(0, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual("A1", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void AttemptToDeleteAllExceptTheFirstTableRow()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteRows(3, 7); // delete 7 rows starting on 0-based row 3
				Assert.AreEqual(1, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(3, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(1, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(3, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H3", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void DeleteRowsWithinTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteRows(5, 2); // delete 2 rows starting on 0-based row 5
				Assert.AreEqual(3, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H5", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range: " + part.Worksheet.SheetDimension.CellArea.Range);
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}
		
		[Test]
		public void DeleteRowsSpanningLowerTableBoundary()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteRows(5, 7); // delete 7 rows starting on 0-based row 5
				Assert.AreEqual(3, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H5", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range: " + part.Worksheet.SheetDimension.CellArea.Range);
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void InsertColumnsLeftOfTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertColumns(1, 5); // insert 5 columns starting on 0-based column 1
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(12, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("H3:M7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value." + part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin);
				Assert.AreEqual(12, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value." + part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End);
			}
		}

		[Test]
		public void InsertColumnsStartingOnFirstTableColumn()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertColumns(2, 5); // insert 5 columns starting on 0-based column 2
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(12, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("H3:M7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value." + part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin);
				Assert.AreEqual(12, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value." + part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End);
			}
		}

		[Test]
		public void InsertColumnsWithinTable()
		{
// This test currently fails because insertion is not implement in TableWrapper
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertColumns(3, 5); // insert 5 columns starting on 0-based column 3
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(12, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(11, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(11, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:M3", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range: " + part.Worksheet.SheetDimension.CellArea.Range);
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(12, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void InsertColumnsRightOfTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.InsertColumns(9, 5); // insert 5 columns starting on 0-based column 9
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void DeleteColumnsLeftOfTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteColumns(0, 2); // delete 2 columns starting on 0-based column 0
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(0, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(5, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("A3:F7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(0, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(5, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void DeleteColumnsSpanningLeftTableBoundary()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteColumns(1, 2); // delete 2 columns starting on 0-based column 1
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(1, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(5, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(5, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("B3:F7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(1, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(5, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void DeleteColumnsSpanningEntireTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteColumns(1, 8); // delete 8 columns starting on 0-based column 1
				Assert.AreEqual(0, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(0, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual("A1", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(0, part.Worksheet.ColumnSets.Count, "Columns not deleted.");
			}
		}

		[Test]
		public void AttemptToDeleteAllExceptTheFirstTableColumn()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteColumns(3, 8); // delete 8 columns starting on 0-based column 3
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(1, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(1, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:C7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void DeleteColumnsWithinTable()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteColumns(5, 2); // delete 2 columns starting on 0-based column 5
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(5, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(4, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:F7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range: " + part.Worksheet.SheetDimension.CellArea.Range);
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(5, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void DeleteColumnsSpanningRightTableBoundary()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];
				part.Worksheet.DeleteColumns(5, 8); // delete 8 columns starting on 0-based column 5
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(3, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(3, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:E7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range: " + part.Worksheet.SheetDimension.CellArea.Range);
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(4, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}






	}
}
